﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jy123.BLL.Props
{
    public class Props
    {
        /// <summary>
        /// 获取所有属于某一根类型的某一组件类型的道具列表（装备也算道具）
        /// </summary>        
        /// <param name="JType">组件的类型</param>
        /// <param name="root">道具的根类型</param>
        /// <returns>道具列表</returns>
        public List<Jy123.Model.Public_Props> GetProps(int JType, string root)
        {
            Public_Props BLLProps = new Public_Props();
            Public_PropsType BLLPropsType = new Public_PropsType();
            List<Jy123.Model.Public_PropsType> listAllType = BLLPropsType.GetModelList(string.Empty);
            List<Jy123.Model.Public_PropsType> listEquipType = new List<Jy123.Model.Public_PropsType>();
            //获取所有属于此根类型的道具子类型
            foreach (Jy123.Model.Public_PropsType propsType in listAllType)
            {
                if (propsType.PTree.Contains(root))//判断是否属于此根类型
                {
                    listEquipType.Add(propsType);
                }
            }
            //获取所有属于此根类型的道具定义
            StringBuilder strPropsWhere = new StringBuilder();
            if (JType > 0)
            {
                strPropsWhere.AppendFormat("FJType = {0} AND (", JType);
            }
            else
            {
                strPropsWhere.Append("(");
            }
            strPropsWhere.AppendFormat("FPType = {0} ", listEquipType[0].ID);
            for (int i = 1; i < listEquipType.Count; i++)
            {
                strPropsWhere.AppendFormat("OR FPType = {0} ", listEquipType[i].ID);
            }
            strPropsWhere.Append(")");
            List<Jy123.Model.Public_Props> listProps = BLLProps.GetModelList(strPropsWhere.ToString());
            return listProps;
        }
        /// <summary>
        /// 获取所有属于某一根类型的某一用户的道具记录（装备也算道具）
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="JType">俱乐部的类型</param>
        /// <param name="root">道具的根类型</param>
        /// <returns>道具记录</returns>
        public List<Jy123.Model.Club_PropsRecord> GetPropsRecord(int userId, int JType, string root)
        {
            Club_PropsRecord BLLPropsRecord = new Club_PropsRecord();
            Public_Props BLLProps = new Public_Props();
            Public_PropsType BLLPropsType = new Public_PropsType();
            List<Jy123.Model.Public_PropsType> listAllType = BLLPropsType.GetModelList(string.Empty);
            List<Jy123.Model.Public_PropsType> listEquipType = new List<Jy123.Model.Public_PropsType>();
            //获取所有属于此根类型的道具子类型
            foreach (Jy123.Model.Public_PropsType propsType in listAllType)
            {
                if (propsType.PTree.Contains(root))//判断是否属于此根类型
                {
                    listEquipType.Add(propsType);
                }
            }
            //获取所有属于此根类型的道具定义
            StringBuilder strPropsWhere = new StringBuilder();
            if (JType > 0)
            {
                strPropsWhere.AppendFormat("FJType = {0} AND (", JType);
            }
            else
            {
                strPropsWhere.Append("(");
            }
            strPropsWhere.AppendFormat("FPType = {0} ", listEquipType[0].ID);
            for (int i = 1; i < listEquipType.Count; i++)
            {
                strPropsWhere.AppendFormat("OR FPType = {0} ", listEquipType[i].ID);
            }
            strPropsWhere.Append(")");
            List<Jy123.Model.Public_Props> listProps = BLLProps.GetModelList(strPropsWhere.ToString());
            //获取所有属于此根类型的该俱乐部的道具记录
            StringBuilder strRecordWhere = new StringBuilder();
            strRecordWhere.AppendFormat("FUser = {0} AND (", userId);
            if (listProps.Count > 0)
            {
                strRecordWhere.AppendFormat("FProps = {0} ", listProps[0].ID);
                if (listProps.Count > 1)
                {
                    for (int i = 1; i < listProps.Count; i++)
                    {
                        strRecordWhere.AppendFormat("OR FProps = {0} ", listProps[i].ID);
                    }
                }
            }
            else
            {
                return new List<Jy123.Model.Club_PropsRecord>();
            }

            strRecordWhere.Append(")");
            List<Jy123.Model.Club_PropsRecord> listRecord = BLLPropsRecord.GetModelList(strRecordWhere.ToString());
            return listRecord;
        }
        /// <summary>
        /// 道具购买
        /// </summary>
        /// <param name="user">组件所有者</param>        
        /// <param name="propsId">道具ID</param>
        /// <param name="quantity">道具的数量</param>
        /// <param name="moduleId">组件ID</param>
        /// <param name="JFather">组件所属类型</param>
        /// <returns>成功失败</returns>
        public bool BuyProps(Jy123.Model.User_Main user, int propsId, int quantity, int moduleId, int JFather)
        {
            try
            {
                User_Main BLLUser = new User_Main();
                Club_PropsRecord BLLRecord = new Club_PropsRecord();
                Public_Props BLLProps = new Public_Props();
                Jy123.Model.Public_Props props = BLLProps.GetModel(propsId);
                string strWhere = "FProps = " + propsId.ToString() + " AND FUser = " + user.ID.ToString();
                List<Jy123.Model.Club_PropsRecord> listRecord = BLLRecord.GetModelList(strWhere);
                Jy123.Model.Club_PropsRecord record;
                if (props.TransactionTimes.HasValue)
                {
                    props.TransactionTimes = props.TransactionTimes.Value + quantity;
                }
                else
                {
                    props.TransactionTimes = quantity;
                }
                BLLProps.Update(props);
                if (listRecord.Count > 0)
                {
                    record = listRecord[0];
                    record.Quantity = record.Quantity.Value + quantity;
                    BLLRecord.Update(record);
                }
                else
                {
                    record = new Jy123.Model.Club_PropsRecord();
                    record.FUser = user.ID;
                    record.FProps = propsId;
                    record.Quantity = quantity;
                    BLLRecord.Add(record);
                }

                //if (JFather == Convert.ToInt32(Public.EnumConfig.JyType.Venues_Main))
                //{
                //    Venues_Main BLLVenues = new Venues_Main();
                //    Model.Venues_Main venues = BLLVenues.GetModel(moduleId);
                //    if (venues.Assets.HasValue)
                //    {
                //        venues.Assets = venues.Assets.Value + props.Price.Value;
                //    }
                //    else
                //    {
                //        venues.Assets = props.Price.Value;
                //    }
                //    BLLVenues.Update(venues);
                //}
                //else if (JFather == Convert.ToInt32(Public.EnumConfig.JyType.Venues_Main))
                //{
                //    Club_Main BLLClub = new Club_Main();
                //    Model.Club_Main club = BLLClub.GetModel(moduleId);
                //    if (club.Assets.HasValue)
                //    {
                //        club.Assets = club.Assets.Value + props.Price.Value;
                //    }
                //    else
                //    {
                //        club.Assets = props.Price.Value;
                //    }
                //    BLLClub.Update(club);
                //}
                //else
                //{

                //}
                user.Cash = Convert.ToInt32((user.Cash.Value - (props.Price.Value * quantity)));
                BLLUser.Update(user);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 俱乐部使用道具
        /// </summary>
        /// <param name="club">俱乐部</param>
        /// <param name="propsRecord">使用的道具（用户所拥有的）</param>
        /// <returns>成功或者失败</returns>
        public bool UseProps(Jy123.Model.Club_Main club, Jy123.Model.Club_PropsRecord propsRecord)
        {
            try
            {
                Club_Main BLLClub = new Club_Main();
                Club_PropsRecord BLLRecord = new Club_PropsRecord();
                Public_Props BLLProps = new Public_Props();
                Jy123.Model.Public_Props props = BLLProps.GetModel(propsRecord.FProps.Value);
                if (props.Liveness.HasValue)
                {
                    club.LiveNess = club.LiveNess.Value + props.Liveness.Value;
                }
                else
                {
                    club.LiveNess = props.Liveness.Value;
                }
                if (props.Moral.HasValue)
                {
                    club.Morale = club.Morale.Value + props.Moral.Value;
                }
                else
                {
                    club.Morale = props.Moral.Value;
                }
                if (props.Skill.HasValue)
                {
                    club.Skill = club.Skill.Value + props.Skill.Value;
                }
                else
                {
                    club.Skill = props.Skill.Value;
                }
                if (props.PhysicalFitness.HasValue)
                {
                    club.PhysicalFitness = club.PhysicalFitness.Value + props.PhysicalFitness.Value;
                }
                else
                {
                    club.PhysicalFitness = props.PhysicalFitness.Value;
                }

                BLLClub.Update(club);
                int quantity = propsRecord.Quantity.Value - 1;
                if (quantity > 0)
                {
                    propsRecord.Quantity = quantity;
                    BLLRecord.Update(propsRecord);
                }
                else
                {
                    BLLRecord.Delete(propsRecord.ID);
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 使用道具给俱乐部加油或捣蛋
        /// </summary>
        /// <param name="user">使用道具的用户</param>
        /// <param name="club">目标俱乐部</param>
        /// <param name="propsId">使用的道具ID</param>
        /// <returns>成功或者失败</returns>
        public bool UsePropsToClub(Model.User_Main user, Model.Club_Main club, Model.Public_Game game, int propsId)
        {
            try
            {
                User_Main BLLUser = new User_Main();
                Club_Main BLLClub = new Club_Main();
                Public_Props BLLProps = new Public_Props();
                Jy123.Model.Public_Props props = BLLProps.GetModel(propsId);
                if (props.Liveness.HasValue)
                {
                    club.LiveNess = club.LiveNess.Value + props.Liveness.Value;
                }
                else
                {
                    club.LiveNess = props.Liveness.Value;
                }
                if (props.Moral.HasValue)
                {
                    club.Morale = club.Morale.Value + props.Moral.Value;
                }
                else
                {
                    club.Morale = props.Moral.Value;
                }
                if (props.Skill.HasValue)
                {
                    club.Skill = club.Skill.Value + props.Skill.Value;
                }
                else
                {
                    club.Skill = props.Skill.Value;
                }
                if (props.PhysicalFitness.HasValue)
                {
                    club.PhysicalFitness = club.PhysicalFitness.Value + props.PhysicalFitness.Value;
                }
                else
                {
                    club.PhysicalFitness = props.PhysicalFitness.Value;
                }
                BLLClub.Update(club);
                Club_Supporter BLLSupporter = new Club_Supporter();
                Model.Club_Supporter supporter = new Jy123.Model.Club_Supporter();
                supporter.FClub = club.ID;
                supporter.FUser = user.ID;
                supporter.ClubName = club.CName;
                supporter.Time = DateTime.Now;
                supporter.UserName = user.UserName;
                supporter.FProps = propsId;
                supporter.FGame = game.ID;
                BLLSupporter.Add(supporter);
                string strWhere = string.Format("FUser = {0} AND FProps = {1}", user.ID.ToString(), propsId.ToString());
                Club_PropsRecord BLLRecord = new Club_PropsRecord();
                List<Model.Club_PropsRecord> listRecord = BLLRecord.GetModelList(strWhere);
                if (listRecord.Count > 0)
                {
                    Model.Club_PropsRecord record = listRecord[0];
                    if (record.Quantity.Value > 1)
                    {
                        record.Quantity = record.Quantity.Value - 1;
                        BLLRecord.Update(record);
                    }
                    else
                    {
                        BLLRecord.Delete(record.ID);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 使用道具给俱乐部进行训练
        /// </summary>
        /// <param name="user">使用道具的用户</param>
        /// <param name="club">目标俱乐部</param>
        /// <param name="propsId">使用的道具ID</param>
        /// <returns>成功或者失败</returns>
        public bool UsePropsToClub(Model.User_Main user, Model.Club_Main club, Model.TCenter_ClubTC game, int propsId)
        {
            try
            {
                User_Main BLLUser = new User_Main();
                Club_Main BLLClub = new Club_Main();
                Public_Props BLLProps = new Public_Props();
                Jy123.Model.Public_Props props = BLLProps.GetModel(propsId);
                if (props.Liveness.HasValue)
                {
                    club.LiveNess = club.LiveNess.Value + props.Liveness.Value;
                }
                else
                {
                    club.LiveNess = props.Liveness.Value;
                }
                if (props.Moral.HasValue)
                {
                    club.Morale = club.Morale.Value + props.Moral.Value;
                }
                else
                {
                    club.Morale = props.Moral.Value;
                }
                if (props.Skill.HasValue)
                {
                    club.Skill = club.Skill.Value + props.Skill.Value;
                }
                else
                {
                    club.Skill = props.Skill.Value;
                }
                if (props.PhysicalFitness.HasValue)
                {
                    club.PhysicalFitness = club.PhysicalFitness.Value + props.PhysicalFitness.Value;
                }
                else
                {
                    club.PhysicalFitness = props.PhysicalFitness.Value;
                }
                BLLClub.Update(club);
                Club_Train BLLTrain = new Club_Train();
                Model.Club_Train supporter = new Jy123.Model.Club_Train();
                supporter.FClub = club.ID;
                supporter.FUser = user.ID;
                supporter.ClubName = club.CName;
                supporter.Time = DateTime.Now;
                supporter.UserName = user.UserName;
                supporter.FProps = propsId;
                supporter.FClubTC = game.ID;
                BLLTrain.Add(supporter);
                string strWhere = string.Format("FUser = {0} AND FProps = {1}", user.ID.ToString(), propsId.ToString());
                Club_PropsRecord BLLRecord = new Club_PropsRecord();
                List<Model.Club_PropsRecord> listRecord = BLLRecord.GetModelList(strWhere);
                if (listRecord.Count > 0)
                {
                    Model.Club_PropsRecord record = listRecord[0];
                    if (record.Quantity.Value > 1)
                    {
                        record.Quantity = record.Quantity.Value - 1;
                        BLLRecord.Update(record);
                    }
                    else
                    {
                        BLLRecord.Delete(record.ID);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 俱乐部使用道具
        /// </summary>        
        /// <param name="club">目标俱乐部</param>
        /// <param name="propsId">使用的道具ID</param>
        /// <returns>成功或者失败</returns>
        public bool UsePropsToClub(Jy123.Model.Club_Main club, int propsId)
        {
            try
            {
                Public_Level BLLLevel = new Public_Level();
                Model.Public_Level level = BLLLevel.GetModel(club.FLevel.Value);
                User_Main BLLUser = new User_Main();
                Club_Main BLLClub = new Club_Main();
                Public_Props BLLProps = new Public_Props();
                Jy123.Model.Public_Props props = BLLProps.GetModel(propsId);
                if (props.Liveness.HasValue)
                {
                    club.LiveNess = club.LiveNess.Value + props.Liveness.Value;
                    if (club.LiveNess.Value > level.PeakLive.Value)
                    {
                        club.LiveNess = level.PeakLive.Value;
                    }
                }
                if (props.Moral.HasValue)
                {
                    club.Morale = club.Morale.Value + props.Moral.Value;
                    if (club.Morale.Value > level.PeakMoral.Value)
                    {
                        club.Morale = level.PeakMoral.Value;
                    }
                }
                if (props.Skill.HasValue)
                {
                    club.Skill = club.Skill.Value + props.Skill.Value;
                    if (club.Skill.Value > level.PeakSkill.Value)
                    {
                        club.Skill = level.PeakSkill.Value;
                    }
                }
                if (props.PhysicalFitness.HasValue)
                {
                    club.PhysicalFitness = club.PhysicalFitness.Value + props.PhysicalFitness.Value;
                    if (club.PhysicalFitness.Value > level.PeakPH.Value)
                    {
                        club.PhysicalFitness = level.PeakPH.Value;
                    }
                }
                BLLClub.Update(club);
                string strWhere = string.Format("FUser = {0} AND FProps = {1}", club.FUser.Value.ToString(), propsId.ToString());
                Club_PropsRecord BLLRecord = new Club_PropsRecord();
                List<Model.Club_PropsRecord> listRecord = BLLRecord.GetModelList(strWhere);
                if (listRecord.Count > 0)
                {
                    Model.Club_PropsRecord record = listRecord[0];
                    if (record.Quantity.Value > 1)
                    {
                        record.Quantity = record.Quantity.Value - 1;
                        BLLRecord.Update(record);
                    }
                    else
                    {
                        BLLRecord.Delete(record.ID);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
